package com.wyz.note.mq.rabbit.provider.config;

import com.wyz.note.mq.rabbit.provider.constant.BaseConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description: 主题交换机
 * @Author: wei yz
 * @Date: 2022/6/12 15:09
 */
@Configuration
public class TopicRabbitConfig {

    //绑定键
    public final static String topicAll = "topic.#";

    @Bean
    public Queue firstQueue() {
        return new Queue(BaseConstant.student);
    }

    @Bean
    public Queue secondQueue() {
        return new Queue(BaseConstant.teacher);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange(BaseConstant.TopicExchange);
    }


    /**
     * 将firstQueue和topicExchange绑定,而且绑定的键值为topic.student
     * 这样只要是消息携带的路由键是topic.student,才会分发到该队列
     *
     * @return
     */
    @Bean
    Binding bindingExchangeMessage() {
        return BindingBuilder.bind(firstQueue()).to(exchange()).with(BaseConstant.student);
    }

    /**
     * 将secondQueue和topicExchange绑定,而且绑定的键值为用上通配路由键规则topic.#
     * 这样只要是消息携带的路由键是以topic.开头,都会分发到该队列
     *
     * @param
     * @return
     * @author wei yz
     */
    @Bean
    Binding bindingExchangeMessage2() {
        return BindingBuilder.bind(secondQueue()).to(exchange()).with(TopicRabbitConfig.topicAll);
    }

}
